package io.renren.modules.gis_admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.ApexUtils;
import io.renren.modules.gis_admin.dao.BasinConfigDao;
import io.renren.modules.gis_admin.dao.BasinRecordDao;
import io.renren.modules.gis_admin.dao.BasinSourceDao;
import io.renren.modules.gis_admin.dto.BasinSourceDTO;
import io.renren.modules.gis_admin.dto.SourceLastResultDTO;
import io.renren.modules.gis_admin.entity.BasinConfigEntity;
import io.renren.modules.gis_admin.entity.BasinRecordEntity;
import io.renren.modules.gis_admin.entity.BasinSourceEntity;
import io.renren.modules.gis_admin.service.BasinSourceService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 源数据
 *
 * @author Mark iu1340@163.com
 * @since 1.0.0 2023-09-21
 */
@Slf4j
@Service
public class BasinSourceServiceImpl implements BasinSourceService {

  @Resource
  BasinSourceDao basinSourceDao;

  @Resource
  BasinConfigDao basinConfigDao;

  @Resource
  BasinRecordDao basinRecordDao;

  @Override
  public Long save(BasinSourceDTO basinSourceDTO) throws IOException {

    ApexUtils.isValidSource(basinSourceDTO.getFilePath());
    ApexUtils.preInit(basinSourceDTO.getFilePath());

    BasinSourceEntity basinSourceEntity = new BasinSourceEntity();
    BeanUtils.copyProperties(basinSourceDTO, basinSourceEntity);

    if (basinSourceEntity.getId() != null) {
      basinSourceDao.updateById(basinSourceEntity);
    } else {
      basinSourceDao.insert(basinSourceEntity);
    }
    return basinSourceEntity.getId();
  }

  @Override
  public List<SourceLastResultDTO> list() {

    QueryWrapper<BasinSourceEntity> queryWrapper = new QueryWrapper<>();
    List<BasinSourceEntity> sourceList = basinSourceDao.selectList(queryWrapper);

    if (CollectionUtils.isEmpty(sourceList)) {
      return null;
    }

    List<SourceLastResultDTO> result = new ArrayList<>();
    // 数据量大的情况下需放到外层一次性查出
    sourceList.forEach(source -> {
      SourceLastResultDTO lastResultDTO = new SourceLastResultDTO();
      Long sourceId = source.getId();
      BeanUtils.copyProperties(source, lastResultDTO);
      try {
        QueryWrapper<BasinRecordEntity> recordQueryWrapper = new QueryWrapper<>();
        recordQueryWrapper.eq("source_id", sourceId);
        recordQueryWrapper.orderByDesc("id");
        List<BasinRecordEntity> recordList = basinRecordDao.selectList(recordQueryWrapper);
        if (CollectionUtils.isEmpty(recordList)) {
          lastResultDTO.setLastConfigList(null);
          lastResultDTO.setLastGisData(null);
        } else {
          BasinRecordEntity lastRecord = recordList.get(0);
          Long lastRecordId = lastRecord.getId();
          lastResultDTO.setId(source.getId());
          QueryWrapper<BasinConfigEntity> configQueryWrapper = new QueryWrapper<>();
          configQueryWrapper.eq("record_id", lastRecordId);
          configQueryWrapper.orderByDesc("basin_id");
          List<BasinConfigEntity> lastConfigList = basinConfigDao.selectList(configQueryWrapper);
          // 最后一次配置
          lastResultDTO.setLastConfigList(lastConfigList);
          // 最后一次结果
          lastResultDTO.setLastGisData(ApexUtils.gisData(lastRecord.getResultPath()));
        }
        result.add(lastResultDTO);
      } catch (Exception e) {
        log.error("get last result failed, sourceId: {} | ", sourceId, e);
      }
    });

    return result;
  }
}